package model;


import state.FarmMachineContext;
import state.IdleState;
import state.MaintenanceState;
import state.WorkingState;

public class Seeder implements FarmMachine {
    private final String deviceId;
    private FarmMachineContext stateContext;
    private boolean isRunning = false;

    public void setRunning(boolean running) {
        isRunning = running;
    }

    public Seeder(String deviceId) {
        this.deviceId = deviceId;
        this.stateContext = new FarmMachineContext();
    }

    @Override
    public void performTask() {
        if (stateContext.getState() instanceof IdleState) {
            if (!isRunning) {
                isRunning = true;
                System.out.println("Seeder " + deviceId + " 启动");
                stateContext.request(); // 触发状态转换
            }
        } else {
            System.out.println("Seeder" + deviceId + " 不在待机状态，无法启动");
        }

    }

    // 实现 EnvironmentObserver 接口的 update 方法
    @Override
    public void update(String sensorType, double value) {
        System.out.println("Seeder " + deviceId + " 接收到传感器更新: " + sensorType + " = " + value);

        // 根据传感器数据调整播种参数
        if (sensorType.equals("土壤湿度")) {
            if (value < 40) {
                adjust("播种深度=浅");
            } else if (value > 60) {
                adjust("播种深度=深");
            }
        } else if (sensorType.equals("温度")) {
            if (value < 15) {
                adjust("播种速度=慢");
            }
        }
    }

    // 实现 AgriculturalDevice 接口的其他方法
    @Override
    public void start() {
        System.out.println("Seeder " + deviceId + " 启动");
    }

    @Override
    public void stop() {
        if (isRunning) {
            isRunning = false;
            System.out.println("Seeder " + deviceId + " 停止");
            stateContext.setState(new IdleState());
        }
    }

    @Override
    public void adjust(String params) {
        System.out.println("Seeder " + deviceId + " 调整参数: " + params);
    }

    @Override
    public void emergencyStop() {
        System.out.println("Seeder " + deviceId + " 紧急停止");
    }

    @Override
    public String getCurrentParams() {
        return "当前参数: 播种深度=中等, 播种速度=正常";
    }

    @Override
    public String getDeviceId() {
        return deviceId;
    }

    @Override
    public boolean isRunning() {
        // 实际实现中应该返回真实的运行状态
        return isRunning;
    }
    @Override
    public String getName() {
        return "播种机";
    }

    @Override
    public String getType() {
        return "SEEDER";
    }

    @Override
    public String getStatus() {
        return isRunning() ? "运行中" : "停止";
    }

    @Override
    public String getStatusDescription() {
        if (stateContext.getState() instanceof IdleState) {
            return "待机";
        } else if (stateContext.getState() instanceof WorkingState) {
            return "运行中";
        } else if (stateContext.getState() instanceof MaintenanceState) {
            return "维护中";
        }
        return "未知状态";
    }

    @Override
    public FarmMachineContext getStateContext() {
        return stateContext;
    }

    @Override
    public void setStateContext(FarmMachineContext stateContext) {
        this.stateContext = stateContext;
    }
}